package com.tim.helper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Slf4j
public class GenDataHelper {
    PreparedStatement depositPs;
    JSONObject depositJson;
    String[] depositColumns;

    String reconDbUrl = "jdbc:mysql://10.1.56.201:3306/reconcile";
    String reconDbUser = "root";
    String reconDbPwd = "AtaV60sc";
    int shardSize = 4;
    PreparedStatement[] reconStatusPsArr = new PreparedStatement[shardSize];
    JSONObject reconStatusJson;
    String[] reconStatusColumns;

    @Before
    public void init() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        {
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://10.1.56.201:3306/inte",
                    "root", "AtaV60sc");
            String columnInfo = ("biz_no,biz_type ")
                    .replaceAll("\\s", "");
            depositColumns = columnInfo.split(",");
            String sql = String.format("INSERT INTO pay_deposit_instruction (%s) VALUEs (%s)",
                    columnInfo, StringUtils.repeat("?", ",", depositColumns.length));
            depositJson = JSON.parseObject("{\n" +
                    "\t\t\"id\":1623909,\n" +
                    "\t\t\"biz_no\":\"300600UAP00D01218B9C7401EC4FCF\",An" +
                    "\t\t\"biz_type\":\"DEPOSIT_DEDUCT\"}");
            depositPs = conn.prepareStatement(sql);
        }

        {
            String columnInfo = ("biz_no,biz_type ")
                    .replaceAll("\\s", "");
            reconStatusColumns = columnInfo.split(",");
            for (int i = 0; i < shardSize; i++) {
                Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://10.1.56.201:3306/reconcile",
                        "root", "AtaV60sc");
                String sql = String.format("INSERT INTO pay_deposit_instruction_%s (%s) VALUEs (%s)",
                        i, columnInfo, StringUtils.repeat("?", ",", reconStatusColumns.length));
                reconStatusPsArr[i] = conn.prepareStatement(sql);
            }
            reconStatusJson = JSON.parseObject("{\n" +
                    "\t\t\"id\":1623909,\n" +
                    "\t\t\"biz_no\":\"300600UAP00D01218B9C7401EC4FCF\",An" +
                    "\t\t\"biz_type\":\"DEPOSIT_DEDUCT\"}");
        }


    }

    @Test
    public void t() throws SQLException {
        int num = 100 * 10000;
        int exeBatch = 5000;
        long currentTimeMillis = System.currentTimeMillis();
        long preMillis = currentTimeMillis;
        for (int j = 0; j < num; j++) {
            if (j % exeBatch == 0) {
                long curMillis = System.currentTimeMillis();
                log.info("exeBatch {),cost={}", j, curMillis - preMillis);
                preMillis = curMillis;
            }
            String biz_no = "400600UAP" + currentTimeMillis + (10000000 + j);
            String request_no = "402900UAA" + currentTimeMillis + (10000000 + j);

            depositJson.put("biz_no", biz_no);
            depositJson.put("request_no", request_no);
            depositJson.put("finish_time", "2025-3-20");
            for (int i = 0; i < depositColumns.length; i++) {
                depositPs.setString(i + 1, depositJson.getString(depositColumns[i]));
            }
            depositPs.addBatch();

            int shardNo = 1;//Crc32Hashs.hash(biz_no.getBytes(Constants.UTF8), shardSize);
            reconStatusJson.put("no", biz_no);
            reconStatusJson.put("matched", "Y");
            for (int i = 0; i < reconStatusColumns.length; i++) {
                reconStatusPsArr[shardNo].setString(i + 1, reconStatusJson.getString(reconStatusColumns[i]));
            }
            reconStatusPsArr[shardNo].addBatch();

            if (j != 0 && j % exeBatch == 0) {
                exe();
            }

        }
        exe();
    }

    private void exe() throws SQLException {
        depositPs.execute();
        depositPs.clearBatch();

        for(int i = 0; i < shardSize; i++){
            reconStatusPsArr[i].execute();
            reconStatusPsArr[i].clearBatch();
        }
    }

}